package com.vogue.supplymall.entmanagement.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.vogue.supplymall.entmanagement.domain.bean.ConstantBean;
import com.vogue.supplymall.entmanagement.domain.dto.LicenceApplyEditDTO;
import com.vogue.supplymall.entmanagement.domain.model.LicenceApply;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.vogue.supplymall.entmanagement.domain.service.LicenceApplyService;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.exception.AttributesNotNullableException;
import com.vogue.supplymall.entmanagement.exception.SaveApplyFailException;
import com.vogue.supplymall.entmanagement.exception.ShopDisposingException;


/**
 * The type Licence apply edit controller.
 */
@Validated
@RestController
public class LicenceApplyEditController extends BaseController {
	/**
	 * The constant logger.
	 */
	public static Logger logger = LoggerFactory.getLogger(LicenceApplyEditController.class);

     @Resource
     private LicenceApplyService licenceApplyService;

	/**
	 * 打开编辑某店铺的申请记录页面
	 *
	 * @param appver  appver
	 * @param shopid  shopid
	 * @param request request
	 * @return ResponseEntity response entity
	 */
	@RequestMapping(value = "/is/{appver}/entmanagement/licenceapply/open/{shopid}", method = RequestMethod.POST)
	public ResponseEntity<Object> applyedit(
			@PathVariable("appver") String appver,
			@PathVariable("shopid") Long shopid,
			HttpServletRequest request) {
		if (shopid == null) {
			//确少必要的参数shopid
			throw new AttributesNotNullableException("shopid");
		}
		LicenceApplyEditDTO applyDTO = licenceApplyService.findShopApply(shopid);
		ControllerResultInfo resultInfo = new ControllerResultInfo();
		resultInfo.setResult(true);
		resultInfo.setData(applyDTO);
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
	}

	/**
	 * 保存申请记录修改
	 *
	 * @param appver  the appver
	 * @param editDTO the edit dto
	 * @param request the request
	 * @return response entity
	 * @throws Exception
	 */
	@RequestMapping(value = "/is/{appver}/entmanagement/licenceapply/save", method = RequestMethod.POST)
     public ResponseEntity<Object> applyesave(@PathVariable("appver") String appver,
		@Validated @RequestBody LicenceApplyEditDTO editDTO, HttpServletRequest request) {
		String shopid = null;
		if (editDTO.getCurrentDTO() != null) {
			shopid = editDTO.getCurrentDTO().getShopid();
		} else {
			if (editDTO.getRenewDTO() != null) {
				shopid = editDTO.getRenewDTO().getShopid();
			}
		}
		if (shopid == null) {
			//确少必要的参数shopid
			throw new AttributesNotNullableException("shopid");
		} else {
			if (ConstantBean.applyConcurrentHashMap.putIfAbsent(shopid, Boolean.TRUE) != null) {
				//该店铺正在处理中就稍候再试
				throw new ShopDisposingException();
			}
		}
		
		ControllerResultInfo resultInfo = new ControllerResultInfo();
		resultInfo.setResult(true);
		try {
			resultInfo.setData(licenceApplyService.saveShopApply(editDTO));
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			throw new SaveApplyFailException();
		} finally {
			ConstantBean.applyConcurrentHashMap.remove(shopid);
		}
		return new ResponseEntity<>(resultInfo, HttpStatus.OK); 
	}

	/**
	 * 删除申请
	 *
	 * @param appver  the appver
	 * @param applyid the applyid
	 * @param request the request
	 * @return response entity
	 */
	@RequestMapping(value = "/is/{appver}/entmanagement/licenceapply/del/{applyid}", method = RequestMethod.POST)
     public ResponseEntity<Object> applyedel(@PathVariable("appver") String appver,
			@PathVariable("applyid") Long applyid, HttpServletRequest request) {
		licenceApplyService.delCurApply(applyid);
		ControllerResultInfo resultInfo = new ControllerResultInfo();
		resultInfo.setResult(true);
		return new ResponseEntity<>(resultInfo, HttpStatus.OK); 
	}

	/**
	 * 审核通过申请(临时方法方便测试，正式管理后台在service层重新定义正式方法)
	 *
	 * @param appver  the appver
	 * @param applyid the applyid
	 * @param request the request
	 * @return response entity
	 */
	@RequestMapping(value = "/is/{appver}/entmanagement/licenceapply/check/{applyid}",
			method = RequestMethod.POST)
	public ResponseEntity<Object> applyecheck(
			@PathVariable("appver") String appver,
			@PathVariable("applyid") Long applyid,
			HttpServletRequest request) {
		LicenceApply apply = licenceApplyService.get(applyid);
		apply.setApplystatue(1);
		licenceApplyService.update(apply);
		ControllerResultInfo resultInfo = new ControllerResultInfo();
		resultInfo.setResult(true);
		return new ResponseEntity<>(resultInfo, HttpStatus.OK);
	}
}
